//
//  SampuxLoginViewController.m
//  Sampux
//
//  Created by rzm on 16/7/1.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxLoginViewController.h"
#import "SampuxRegisterViewController.h"
#import "SPEquipmentListViewController.h"
#import "SampuxReminderViewController.h"
#import "GifView.h"
#import "GBInterMaster_Launcher.h"

@interface SampuxLoginViewController ()<UIWebViewDelegate,LoginViewAllBtnClickDelegate>
{
    NSTimer *timer;
}
@property (nonatomic, strong) UIWebView *startView ;
@property (nonatomic, strong) GifView *gifView;

@end

@implementation SampuxLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createGifView];
}

- (void)createGifView
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sampux" ofType:@"gif"];
    
    _gifView = [[GifView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) filePath:path];
    _gifView.backgroundColor = [UIColor whiteColor];
    _gifView.userInteractionEnabled = YES ;
    [self.view addSubview:_gifView];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
}

- (void)createWebView
{
    NSBundle *goodBundle = [NSBundle bundleWithPath: [[NSBundle mainBundle] pathForResource:@"startWeb" ofType:@"bundle"]];
    NSURL *url = [goodBundle URLForResource:@"index" withExtension:@"html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    _startView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    [self.view addSubview:_startView];
    [_startView loadRequest:request];
    _startView.delegate = self;
    [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
}

- (void)fadeScreen
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(finishedFading)];
    self.view.alpha = 0.0;
    [UIView commitAnimations];
    [timer invalidate];
    timer = nil;
}

- (void)finishedFading
{
    [UIView animateWithDuration:0 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [_startView removeFromSuperview];
        [self createLoginView];
        self.view.alpha = 1.0;
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)createLoginView
{
    _loginView = [[SampuxLoginView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_loginView];
    _loginView.btnClickDelegate = self;
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
//    [delegate.launcher connectToServer];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBarHidden = NO;
}

- (void)clickLoginBtn
{
    dispatch_async(dispatch_get_main_queue(), ^{
        AppDelegate *delegate = [UIApplication sharedApplication].delegate;
        SPEquipmentListViewController *listVC = [[SPEquipmentListViewController alloc]init];
        
        SampuxCustomNavController *nav = [[SampuxCustomNavController alloc]initWithRootViewController:listVC];
        delegate.window.rootViewController = nav;

    });
//    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:listVC];
}

- (void)clickRegisterBtn
{
    SampuxRegisterViewController *registerVC = [[SampuxRegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

- (void)clickforgetPasswordBtn
{
    SampuxReminderViewController *reminderVC = [[SampuxReminderViewController alloc]init];
    [self.navigationController pushViewController:reminderVC animated:YES];
}

@end
